//
//  YCSellCardsViewController.m
//  YCVillageDoctor
//
//  Created by zhangxiaoliang on 2017/6/7.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCSellCardsViewController.h"

#import "YCSellRightTableViewCell.h"
#import "YCSellRightModel.h"
#import "YCDetailHtmlViewController.h"
#import "YCSellTypeModel.h"
#import "YCSellLeftTableViewCell.h"
#import "YCHMOInfoViewController.h"
#import "YCGroupModel.h"
#define kLeftListW 100

@interface YCSellCardsViewController ()<UITableViewDelegate,UITableViewDataSource>



//数据源
@property(nonatomic,strong)NSArray *allData;

@property(nonatomic,strong)UITableView *tableViewLeft;
@property(nonatomic,strong)UITableView *tableViewRight;

@property(nonatomic,strong)UIView *headerView;

@property(nonatomic,strong)UIImageView *imageViewH;

@property(nonatomic,assign)NSInteger cuuentSelectIndex;
@end

@implementation YCSellCardsViewController
- (void)viewDidLoad {
    [super viewDidLoad];
   
    _cuuentSelectIndex = 0;
    
    UITableView *tableViewLeft = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kLeftListW, kScreenHeight - 64 - self.tabBarController.tabBar.height - 44) style:UITableViewStyleGrouped];
    self.tableViewLeft = tableViewLeft;
    tableViewLeft.dataSource = self;
    tableViewLeft.delegate  = self;
    tableViewLeft.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableViewLeft.backgroundColor = RGB(247, 247, 249);
    [self.view addSubview:tableViewLeft];
    
    
    
    
    UITableView *tableViewRight = [[UITableView alloc]initWithFrame:CGRectMake(kLeftListW, 0, kScreenWidth - kLeftListW, tableViewLeft.height) style:UITableViewStyleGrouped];
    self.tableViewRight = tableViewRight;
    tableViewRight.dataSource = self;
    tableViewRight.delegate  = self;
    tableViewRight.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableViewRight.backgroundColor = RGB(247, 247, 249);
    tableViewRight.tableFooterView = [UIView new];
    [self.view addSubview:tableViewRight];
    
    
    
    self.view.backgroundColor = RGB(247, 247, 249);
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - kLeftListW, 110)];
    headerView.backgroundColor = RGB(247, 247, 249);
    UIImageView *imageViewH = [[UIImageView alloc]init];
    imageViewH.frame = CGRectMake(10, 10, kScreenWidth - kLeftListW - 20, 85);
    self.imageViewH = imageViewH;
    [headerView addSubview:imageViewH];
    self.headerView = headerView;
    
    self.tableViewRight.tableHeaderView = headerView;
    
    
    
    UIButton *btnDetail = [[UIButton alloc]initWithFrame:CGRectMake(0, kScreenHeight - self.tabBarController.tabBar.height - 44 - 64, kScreenWidth, 44)];
    
    [btnDetail setTitle:@"《HMO卡使用须知》" forState:UIControlStateNormal];
    [btnDetail setTitleColor:RGB(112, 156, 241) forState:UIControlStateNormal];
    btnDetail.titleLabel.font = [UIFont systemFontOfSize:15];
    [btnDetail addTarget:self action:@selector(btnDetail) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnDetail];
    

    

    [DownloadData post:kGetHMOList params:@{@"member_id":[YCUserTool sharedTools].expertID ?:@"485638"} success:^(id json) {
        [self.view removeTipView];
        self.allData = [YCSellTypeModel mj_objectArrayWithKeyValuesArray:json];
        [self.tableViewLeft reloadData];
        [self.tableViewRight reloadData];
        
        [self.imageViewH sd_setImageWithURL:[NSURL URLWithString:[[self.allData firstObject] card_pic]]];
        
        [self.tableViewLeft selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        
    } failure:^(NSError *error) {
        [self.view errorTip:^{
            [DownloadData post:kGetHMOList params:@{@"expert_id":[YCUserTool sharedTools].expertID,@"page_num":@"1"} success:^(id json) {
                [self.view removeTipView];
                self.allData = [YCSellTypeModel mj_objectArrayWithKeyValuesArray:json];
                [self.tableViewLeft reloadData];
                [self.tableViewRight reloadData];
                [self.imageViewH sd_setImageWithURL:[NSURL URLWithString:[[self.allData firstObject] card_pic]]];
                [self.tableViewLeft selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                
            } failure:^(NSError *error) {
            }];
            
        }];
    }];
    
    
    __weak __typeof__(self)weakSelf = self;
    [DownloadData post:kGetQRCodeInfo params:@{@"expert_id":[YCUserTool sharedTools].expertID} success:^(id json) {
        [weakSelf dismissTips];
        [YCUserTool sharedTools].userID = json[@"user_id"]?:@"";
        [YCUserTool sharedTools].userName = json[@"user_name"]?:@"";
        
    } failure:^(NSError *error) {
        [weakSelf dismissTips];
    }];
    
    

}

- (void)btnDetail
{
    YCHMOInfoViewController *infoVc = [[YCHMOInfoViewController alloc]init];
    [self.navigationController pushViewController:infoVc animated:YES];
}
#pragma mark - tableView代理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableViewLeft == tableView) {
        static NSString *ID = @"cellLeft";
        YCSellLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[YCSellLeftTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        YCSellTypeModel *model = self.allData[indexPath.row];
        cell.textLabel.text =  model.yw_name;
        return cell;
    }
    if (self.tableViewRight == tableView) {
        static NSString *ID = @"cellRight";
        YCSellRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[YCSellRightTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
            
        }
        YCSellTypeModel *model = self.allData[_cuuentSelectIndex];
        YCGroupModel* groupModel = model.data[indexPath.section];
        cell.model = groupModel.data[indexPath.row];
        if (indexPath.row == groupModel.data.count - 1) {
            cell.isShowLine = YES;
        }else
        {
           cell.isShowLine = NO;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableViewLeft) {
        return 1;
    }else if(tableView == self.tableViewRight)
    {
        YCSellTypeModel *model = self.allData[_cuuentSelectIndex];
        return [model.data count];
    }else{
        return 0;
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableViewLeft) {
        return self.allData.count;
    }else{
        YCSellTypeModel *model = self.allData[_cuuentSelectIndex];
        YCGroupModel *groupModel = model.data[section];
        return groupModel.data.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableViewLeft) {
        return 50;
    }
    
    if (tableView == self.tableViewRight) {
        return 80;
    }
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tableViewLeft) {
        return 0.01;
    }else if (tableView == self.tableViewRight)
    {
        return 25;
    }else{
        return 0;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tableViewLeft) {
        return nil;
    }else if(tableView == self.tableViewRight){
        UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.width , 30)];

        myView.backgroundColor = RGB(247, 247, 247);
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0,tableView.width, 25)];
        titleLabel.textColor=[UIColor blackColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont systemFontOfSize:14];
        [myView addSubview:titleLabel];
        YCSellTypeModel *model = self.allData[_cuuentSelectIndex];
        YCGroupModel *groupModel = model.data[section];
        titleLabel.text =  groupModel.group;
        return myView;
    }else{
    
        return nil;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableViewLeft) { //点击了左边  跳转刷新右边
        _cuuentSelectIndex = indexPath.row;
        YCSellTypeModel *model = self.allData[indexPath.row];
        [self.imageViewH sd_setImageWithURL:[NSURL URLWithString:[model card_pic]]];
        [self.tableViewRight reloadData];
    }else if(tableView == self.tableViewRight){ //点击了右边
        YCDetailHtmlViewController *detailVc = [[YCDetailHtmlViewController alloc]init];
        YCSellTypeModel *modelType =  self.allData[_cuuentSelectIndex];
        YCGroupModel *groupModel = modelType.data[indexPath.section];
        detailVc.model = groupModel.data[indexPath.row];
        [self.navigationController pushViewController:detailVc animated:YES];
    }else{
    }
 
}

@end
